﻿// Fill out your copyright notice in the Description page of Project Settings.


#include "Character/BossCharacter.h"

#include "BrainComponent.h"
#include "AIController/BossAIController.h"
#include "Character/PlayerCharacter.h"
#include "Components/CapsuleComponent.h"
#include "Components/WidgetComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "GameMode/CombatGameMode.h"
#include "Kismet/GameplayStatics.h"
#include "Kismet/KismetSystemLibrary.h"


// Sets default values
ABossCharacter::ABossCharacter()
{
	// Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	bUseControllerRotationPitch = false;
	bUseControllerRotationRoll = false;
	bUseControllerRotationYaw = false;

	UCharacterMovementComponent* CharacterMovementComponent = GetCharacterMovement();
	// 设置血条组件
	// WidgetComponent = CreateDefaultSubobject<UWidgetComponent>("HealthBar");
	// WidgetComponent->SetupAttachment(RootComponent);
	CharacterMovementComponent->bUseControllerDesiredRotation = true;
}

// Called when the game starts or when spawned
void ABossCharacter::BeginPlay()
{
	Super::BeginPlay();
	//把血条widget交给组件
	// UHealthBarWidget* HealthBarWidget = CreateWidget<UHealthBarWidget>(GetWorld(), HealthBarWidgetClass);
	// WidgetComponent->SetWidget(HealthBarWidget);
	// HealthBarWidget->Owner = this;
}

// Called every frame
void ABossCharacter::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

// Called to bind functionality to input
void ABossCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);
}

void ABossCharacter::PlayAttackMontage()
{
	IsAttacking_AI = true;
	PlayMontage(AttackMontage);
}

void ABossCharacter::AttackDamage()
{
	// Super::AttackDamage();
	// Debug::printLog("player do attackDamage");
	FVector start = this->GetActorLocation();
	FVector end = start + this->GetActorForwardVector() * 200;
	TArray<FHitResult> OutHits;
	const TArray<AActor*> ActorsToIgnore;
	bool bSphereTraceMultiByProfile = UKismetSystemLibrary::SphereTraceMultiByProfile(
		GetWorld(), start, end, AttackRadius, "",
		true, ActorsToIgnore, EDrawDebugTrace::None, OutHits, true,
		FLinearColor::Blue, FLinearColor::Red, 5.0f);
	TArray<ABaseCharacter*> haveHit;
	if (bSphereTraceMultiByProfile)
	{
		for (FHitResult res : OutHits)
		{
			APlayerCharacter* player = Cast<APlayerCharacter>(res.GetActor());
			if (player && !haveHit.Contains(player))
			{
				player->TakeHitDamage(10.0f);
				haveHit.AddUnique(player);
			}
		}
	}
}

void ABossCharacter::TakeHitDamage(const float DamageValue)
{
	Super::TakeHitDamage(DamageValue);

	ABossAIController* enemyAIController = Cast<ABossAIController>(GetController());
	if (enemyAIController)
	{
		enemyAIController->SetAIDamage();
	}
}

void ABossCharacter::DeathClear()
{
	//血条widget组件销毁
	// WidgetComponent->DestroyComponent();
	// 停止ai控制
	ACombatGameMode* GameMode = Cast<ACombatGameMode>(UGameplayStatics::GetGameMode(GetWorld()));
	ABossAIController* BossAIController = Cast<ABossAIController>(GetController());
	if (BossAIController)
	{
		
		UBrainComponent* BrainComponent = BossAIController->BrainComponent;
		BrainComponent->StopLogic("");
		BossAIController->ClearFocus(EAIFocusPriority::Default);
	}
	// 停止运动
	GetCharacterMovement()->SetMovementMode(MOVE_None);
	GetCapsuleComponent()->SetCollisionEnabled(ECollisionEnabled::Type::NoCollision);
	GameMode->MainUI->BossHealthBarPanel->SetVisibility(ESlateVisibility::Hidden);
	GameMode->ChangeToGameOverUI(FText::FromString("Vectory"));
	// 销毁自身
	FLatentActionInfo info;
	info.Linkage = 1;
	info.UUID = 100;
	info.CallbackTarget = this;
	info.ExecutionFunction = "DestroySelf";
	UKismetSystemLibrary::Delay(GetWorld(), 3.0f, info);
}

void ABossCharacter::DestroySelf()
{
	//Debug::printLog("Destroy");
	Destroy();
}

